<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\FileLog;
use app\common\model\VisitShow;
use app\common\model\Help;
use think\facade\View;
use think\facade\Config;
use think\facade\Cache;
use app\common\controller\Handle;
use think\facade\Request;


class Helps extends Base
{
    // 帮助列表
    public function list()
    {
        if (request()->IsAjax()) {
            $get = $this->request->get();
            $limit = isset($get['limit']) && $get['limit'] ? $get['limit'] : 10;
            $page = isset($get['page']) && $get['page'] ? $get['page'] : 1;
            $HelpModel = new Help();
            $sort = 'id desc';
            $helplist = $HelpModel->getPagelist([], '', $sort, $limit, $page);

            foreach($helplist as $key=>$val){
                if($val['type'] == 1){
                    $helplist[$key]['type_name'] = "视频";
                }else{
                    $helplist[$key]['type_name'] = "文本";
                }
            }
            $count = $HelpModel->countRows([]);
            return json(["code" => 0, "msg" => '成功', "count" => $count, "data" => $helplist]);
        } else {
            return View::fetch();
        }
    }
    // 发布帮助
    public function saveHelp()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if(empty($post['name'])){
                return json(['code' => 0, 'msg' => '名称']);
            }

            $add = [
                "name"=>$post['name'],
                "type"=>$post['type'],
                "created_at"=>date("Y-m-d H:i:s"),
                "update_at"=>date("Y-m-d H:i:s"),
            ];

            if($post['type'] == 1){
                if(empty($post['videoUrl'])){
                    return json(['code' => 0, 'msg' => '请上传视频路径']);
                }
                $add['video_url'] = $post['videoUrl'];
            }else{
                if(empty($post['content'])){
                    return json(['code' => 0, 'msg' => '请上传富文本内容']);
                }
                $add['content'] = $post['content'];
            }

            // 判断修改
            if (isset($post['id']) && $post['id']) {
                $add['id'] = $post['id'];

                    // 新增
                try {
                    Help::update($add);
                    return json(['code' => 1, 'msg' => '更新成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '更新失败']);
                }
            } else {
                // 新增
                try {
                    Help::create($add);
                    return json(['code' => 1, 'msg' => '新增成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            }
            
        } else {
            return View::fetch();
        }
    }

     // 修改帮助
     public function editHelp()
     {
           // 存储空间对应的域名
         $domain = config('sysconfig.qiniu_domain');
         $get  = $this->request->get();
         if (isset($get['id']) && $get['id']) {
             $one =  Help::find($get['id']);
             $one->video_url = $domain."/".$one['video_url'];
             View::assign('one', $one);
             return View::fetch();
         } else {
             return json(['code' => 0, 'msg' => '缺少参数']);
         }
     }
    // 删除帮助
    public function delHelp()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if (isset($post['id']) && $post['id']) {
                try {
                    Help::destroy($post['id']);
                    return json(['code' => 1, 'msg' => '删除成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '删除失败']);
                }
            } else {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
        }
    }

     // 上传商品图片或者视频
     public function addFile()
     {
         $file = request()->file('file');
 
         if (!$file) {
             return ['status' => 0, 'msg' => '上传失败，图片不存在'];
         }
 
         // 获取文件原始扩展名
         $extension = $file->extension();
 
         $hand = new Handle();
         $FileLog = new FileLog();
 
         $dirname = "api_help";
 
         $savename = $hand->qiniuImg("file", $dirname);
 
         $add['file_name'] = $savename['fileName'];
         $add['upload_type'] = 2;
         $add['file_path'] = $savename['url'];
         $add['file_type'] = $extension;
         $add['ip'] = Request::ip();
         $add['add_time'] = time();

         
         $res = $FileLog->create($add);
         $fileLogId = $res->id;
         $savename['file_id'] = $fileLogId;
 
         return  json($savename);
     }
 
}
